


#include <iostream>
#include <vector>
#include <algorithm>
using namespace std;

class Solution
{
public:
    int longestOnes1(vector<int>& nums, int k)
    {
        int sz = nums.size(), len = 0;

        int cnt = 0;
        for (auto i : nums)
        {
            if (0 == i)
                ++cnt;
        }

        if (cnt <= k)
            return sz;

        for (int left = 0; left < sz; ++left)
        {
            int curcnt = 0;
            int right = left;
            for (right = left; right < sz; ++right)
            {
                if (0 == nums[right])
                    ++curcnt;

                if (curcnt == k + 1)
                {
                    len = max(len, right - left);
                    break;
                }
            }
            if (right == sz)
                len = max(len, sz - left);
        }

        return len;
    }

    int longestOnes2(vector<int>& nums, int k)
    {
        int sz = nums.size(), maxlen = 0;

        int cnt = 0;
        int left = 0, right = 0;
        while (right < sz)
        {
            if (0 == nums[right])
                ++cnt;

            while (cnt == k + 1)
            {
                if (0 == nums[left])
                    --cnt;
                ++left;
            }
            maxlen = max(maxlen, right - left + 1);

            ++right;
        }

        return maxlen;
    }
};

void Test1()
{
	unsigned short c1 = 65535;
	//short c2{ 65536 };

	cout << c1 << endl;
}

void Test2()
{
    vector<int> nums{ 1,1,1,0,0,0,1,1,1,1 };
    int k = 0;

    cout << Solution().longestOnes2(nums, k) << endl;
}

int main()
{
	//Test1();
    Test2();

	return 0;
}